package com.wteam.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wteam.config.JWT.jwt;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

@Component
public class MyMataObjechandler implements MetaObjectHandler {

    @Autowired
    private HttpServletRequest httpServletRequest;
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("CreateTime", LocalDateTime.now());
  metaObject.setValue("UpdateTime",LocalDateTime.now());

if (httpServletRequest.getHeader("token")!=null){
        Map<String, Object> claims = jwt.parseJWT(httpServletRequest.getHeader("token"));
        String h = (String) claims.get("username");
  metaObject.setValue("UpdateBy",h);
  metaObject.setValue("CreateBy",h);}


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("UpdateTime",LocalDateTime.now());
        if (httpServletRequest.getHeader("token")!=null) {
            Map<String, Object> claims = jwt.parseJWT(httpServletRequest.getHeader("token"));
            String h = (String) claims.get("username");
            metaObject.setValue("UpdateBy", h);
        }
    }




}
